import re

class MySeperator:
    def __init__(self, res = {}):
        self.res = res

    def _split_line(self, line):
        words = re.split('\W', line)
        return words

    def _count_eachline(self, line):
        splited_line = self._split_line(line)
        for word in splited_line:
            if word:
                if word in self.res.keys():
                    self.res[word] += 1
                else:
                    self.res[word] = 1
        return self.res

    def reader(self, filename):
        with open(filename, 'r', encoding='utf-8') as f:
            for line in f:
                self._count_eachline(line)
        result = sorted(self.res.items(), key=lambda x:x[1], reverse=True)[:10]
        return result

sep = MySeperator()
res = sep.reader('sample.txt')
print(res)


# 做得不错，不过可以考虑再做效率优化
